<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	门店
*/
class store extends \plugin{
	public function create($params){
		$postdata=$params;
		$wxdata=array(
			'business_name'=>trim($postdata['title']),
			'introduction'=>trim($postdata['desc']),
			'telephone'=>trim($postdata['tel']),
			'address'=>trim($postdata['address']),
			'branch_name'=>trim($postdata['branch_name']),
			'province'=>$postdata['pcd']['province'],
			'city'=>$postdata['pcd']['city'],
			'district'=>$postdata['pcd']['district'],
			'offset_type'=>3,
			'longitude'=>$postdata['lat_lng']['lng'],
			'latitude'=>$postdata['lat_lng']['lat'],
			'photo_list'=>ijson_encode($postdata['hdps']),
			'recommend'=>trim($postdata['recommend']),
			'special'=>trim($postdata['special']),
			'open_time'=>trim($postdata['onlinetime']),
			'avg_price'=>trim($postdata['avg_price']),
			'out_pay'=>$postdata['out_pay'],
			'out_juli'=>$postdata['out_juli'],
			'businessimg'=>$postdata['businessimg'][0],
			'licenseimg'=>$postdata['licenseimg'][0],
			'api_sid'=>trim($postdata['api_sid']),
			'mid'=>intval($postdata['mid']),
			'salenum'=>intval($postdata['salenum']),
		);
		$postdata['pcd']=ijson_encode($postdata['pcd']);
		$postdata['onlinetime']=ijson_encode($postdata['onlinetime']);
		$postdata['hdps']=ijson_encode($postdata['hdps']);
		$postdata['lat_lng']=ijson_encode($postdata['lat_lng']);
		$data=array_fields(array('jifen_rate','order_rate','workersale','salenum','api_sid','uname','mid','is_work_time','work_time','businessimg','licenseimg','recommend','special','avg_price','title','logo','pcd','lat_lng','address','tel','type','realname','mobile','onlinetime','hdps','desc','content','paixu','status','out_pay','out_juli'),$postdata);
		$data['businessimg']=ijson_encode($data['businessimg']);
		$data['licenseimg']=ijson_encode($data['licenseimg']);
		if($postdata['upass']){
			$data['upass']=base64_encode($postdata['upass']);
		}
		if($data['uname']){
			$uname=pdo_get('jy_weishop_store',array('uname'=>$data['uname'],'uniacid'=>$_SESSION['uniacid']));
		}
		$id=intval($postdata['id']);
		if($id){
			if($uname && $uname['id']!=$id){
				exi('店长账号不可以重复','error');	
			}
			pdo_update('jy_weishop_store',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$wxdata['sid']=$id;
			model('core/wechat_store')->updatestore($wxdata);
		}else{
			if($uname){
				exi('店长账号不可以重复','error');	
			}
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_weishop_store',$data);
			$id=pdo_insertid();
			$wxdata['sid']=$id;
			model('core/wechat_store')->addstore($wxdata);
		}
		return $id;
	}
	public function create_upwechat($params){
		$postdata=$params;
		$postdata['pcd']=array(
			'province'=>$postdata['province'],
			'city'=>$postdata['city'],
			'district'=>$postdata['district'],
		);
		$postdata['pcd']=ijson_encode($postdata['pcd']);
		$postdata['lat_lng']=array('lng'=>$postdata['longitude'],'lat'=>$postdata['latitude']);
		$postdata['lat_lng']=ijson_encode($postdata['lat_lng']);
		$data=array(
			'title'=>trim($postdata['business_name']),
			'desc'=>trim($postdata['introduction']),
			'tel'=>trim($postdata['telephone']),
			'address'=>trim($postdata['address']),
			'pcd'=>$postdata['pcd'],
			'lat_lng'=>$postdata['lat_lng'],
			'hdps'=>ijson_encode($postdata['photo_list']),
			'recommend'=>trim($postdata['recommend']),
			'special'=>trim($postdata['special']),
			'onlinetime'=>trim($postdata['open_time']),
			'avg_price'=>trim($postdata['avg_price']),
			'status'=>1,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_store',$data);
		$id=pdo_insertid();
		return $id;
	}
	public function create_towechat($params){
		$postdata=$params;
		$wxdata=array(
			'business_name'=>trim($postdata['title']),
			'introduction'=>trim($postdata['desc']),
			'telephone'=>trim($postdata['tel']),
			'address'=>trim($postdata['address']),
			'branch_name'=>trim($postdata['branch_name']),
			'province'=>$postdata['pcd']['province'],
			'city'=>$postdata['pcd']['city'],
			'district'=>$postdata['pcd']['district'],
			'offset_type'=>3,
			'longitude'=>$postdata['lat_lng']['lng'],
			'latitude'=>$postdata['lat_lng']['lat'],
			'photo_list'=>ijson_encode($postdata['hdps']),
			'recommend'=>trim($postdata['recommend']),
			'special'=>trim($postdata['special']),
			'open_time'=>trim($postdata['onlinetime']),
			'avg_price'=>trim($postdata['avg_price']),
		);
		$wxdata['sid']=$postdata['id'];
		model('core/wechat_store')->addstore($wxdata);
		return true;
	}
	public function out_times($store_id,$post=array()) {
		$store=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
		$store['onlinetime']=json_decode($store['onlinetime'],true);
		$store['out_pay']=$post['out_pay']?:$store['out_pay'];
		$store['out_pay']=price($store['out_pay']);
		//配送时间
		$days = array(
				0=>date("m-d",TIMESTAMP),
				1=>date("m-d",strtotime("+1 day")),
				2=>date("m-d",strtotime("+2 day")), 
				3=>date("m-d",strtotime("+3 day")), 
				4=>date("m-d",strtotime("+4 day")),
				5=>date("m-d",strtotime("+5 day")),
				6=>date("m-d",strtotime("+6 day")),
		);
		$totaytime = strtotime(date('Y-m-d'));
		//配送时间段
		$lasttimebox=0;
		$daytime=date('Y-m-d');
		if($post['set_timebox']==1){
			foreach($post['peisong_timebox'] as &$item){
				$start=$daytime.' '.$item['times']['start'];
				$start=strtotime($start);
				$end=$daytime.' '.$item['times']['end'];
				$end=strtotime($end);
				$times[]=array(
					'start' => $item['times']['start'],
					'end' => $item['times']['end'],
					'timestamp' => $end,
					'fee' => 0,
					'timev' =>$item['times']['start'].'~'.$item['times']['end'],
					'out_pay' => $store['out_pay']."元配送费",
				);
				$timestamp[] = $end;
				if($end>$item['yuyue']*60+time()){
					$lasttimebox +=1;
				}
			}
		}else{
			$timestamp = array();
			if($store['onlinetime']['start']){
				$start=$daytime.' '.$store['onlinetime']['start'];
				$start=strtotime($start);
			}else{
				$start = mktime(8, 0);
			}
			if($store['onlinetime']['end']){
				$end=$daytime.' '.$store['onlinetime']['end'];
				$end=strtotime($end);
			}else{
				$end = mktime(22, 0);
			}
			for($i = $start; $i < $end;) {
					$times[] = array(
						'start' => date('H:i', $i),
						'end' => date('H:i', $i + 1800),
						'timestamp' => $i + 1800,
						'fee' => 0,
						'timev' =>date('H:i', $i).'~'.date('H:i', $i + 1800),
						'out_pay' => $store['out_pay']."元配送费",
					);
					$timestamp[] = $i + 1800;
					$i += 1800;
					if($i + 1800>30*60+time()){
						$lasttimebox +=1;
					}
			}
		}
		$data = array(
			'days' => $days,
			'times' => $times,
			'timestamp' => $timestamp,
			'updatetime' => strtotime(date('Y-m-d')) + 86400,
			'reserve' => 0,
			'lasttimebox' => $lasttimebox,
		);
		return $data;
	}
	/*
		咨询时间
	*/
	public function zxout_time($dayid=0){
		$nowtime=time();
		$nowweekid=date("w");
		$zxout_time['nowday']=date('Y-m-d');
		$zxout_time['nowday2']='今天';
		$weekarray=array('日','一','二','三','四','五','六');
		foreach($weekarray as $k=>$v){
			$id=0;
			if($nowweekid>$k){
				$id=$k-$nowweekid+7;
			}else{
				$id=$k-$nowweekid;
			}
			$time=$nowtime+$id*24*3600;
			if($nowweekid==$k){
				$day2='今天';
			}else{
				$day2=date('md',$time);
			}
			if($dayid && $dayid==$id){
				$zxout_time['nowday']=date('Y-m-d',$time);
				$zxout_time['nowday2']=$day2;
			}
			$work_weekarray[]=array(
				'id'=>$id,
				'title'=>$v,
				'time'=>$time,
				'day'=>date('Y-m-d',$time),
				'day2'=>$day2,
			);
		}
		$store_id=intval($_RQ['store_id'])?:$_SESSION['store_id'];
		$citypeisong=$this->plugin_setting('citypeisong');
		$post['set_timebox']=$citypeisong['set_timebox'];
		$post['peisong_timebox']=$citypeisong['peisong_timebox'];
		$post['out_pay']=$_RQ['out_pay'];
		$out_time = model('jy_weishop/store')->out_times($store_id,$post);
		$times=$out_time['times'];
		foreach($work_weekarray as &$wv){
			$out_times=$times;
			foreach($out_times as &$ov){
				$out_time=$wv['day'].' '.$ov['timev'];
				$orderids=pdo_getall('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'out_time'=>$out_time),'orderid','orderid');
				if($orderids){
					$orderids=array_keys($orderids);
					$isbuy=pdo_get('jy_weishop_order_goods',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderids,'goodsid'=>$return['goods']['id']),'id');
					if($isbuy['id']){
						$ov['isbuy']=1;
					}
				}
			}
			$wv['out_times']=$out_times;
		}
		$zxout_time['out_weeks']=$work_weekarray;
		$zxout_time['out_times']=$times;
		return $zxout_time;
	}
	/*
		整理获取结束时间下标值
	*/
	public function array_order($value, $array)
	{
		$array[] = $value;
		asort($array);
		$array = array_values($array);
		$index = array_search($value, $array);
		return $array[$index + 1];
	}
	/*
		一键同步更新商品数据
	*/
	public function upgoods($up=false,$page=0,$pluginname='',$nototal=0){
		$page=$page?:1;
		$pindex = max(1, intval($page));
		$psize = 100;
		$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0,'status'=>1);
		if($pluginname){
			$params['onepluginname']=$pluginname;
		}else{
			$params['onepluginname OR']='onepluginname=\'\' or onepluginname=\'jy_weishop\'';
		}
		$orderby='createtime desc';
		$goods=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,array('id','price','total'),'',$orderby);
		foreach($goods as $item){
			$stores=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id'));			
			foreach($stores as $s){
				$isadd=pdo_count('jy_weishop_store_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$item['id'],'storeid'=>$s['id']));
				//还没有了该商品
				if($up && $isadd){
						$data=array(
							'goodsid'=>intval($item['id']),
							'storeid'=>intval($s['id']),
							'total'=>intval($item['total']),
							'price'=>$item['price'],
						);
						if($nototal){
							unset($data['total']);
						}
						pdo_update('jy_weishop_store_goods',$data,array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$item['id'],'storeid'=>$s['id']));
						$this->up_options($item['id'],$s['id'],$up);
				}elseif(!$isadd){
					$data=array(
						'goodsid'=>intval($item['id']),
						'storeid'=>intval($s['id']),
						'total'=>intval($item['total']),
						'price'=>$item['price'],
						'pluginname'=>$item['pluginname']?:$item['onepluginname'],
					);
					if($nototal){
						unset($data['total']);
					}
					$data['uniacid']=$_SESSION['uniacid'];
					pdo_insert('jy_weishop_store_goods',$data);
					$this->up_options($item['id'],$s['id'],$up);
				}
			}
		}
		if(count($goods)>98){
			$page++;
			$url='jy_weishop/store.totalset/pigoods/page='.$page;
			if($up){
			    $url .='&up='.$up;
			}
			exi('开始同步下一批100个商品！请勿关闭网页！','',$url);
		}
		return true;
	}
	/*
		单门店一键同步更新商品数据
	*/
	public function uponestoregoods($storeid,$up=false,$nototal=0){
		$goods=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0,'status'=>1),array('id','price','total','pluginname','onepluginname'));
		foreach($goods as $item){
			$isadd=pdo_count('jy_weishop_store_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$item['id'],'storeid'=>$storeid));
			//还没有了该商品
			if(!$isadd){
					$data=array(
						'goodsid'=>intval($item['id']),
						'storeid'=>intval($storeid),
						'total'=>intval($item['total']),
						'price'=>$item['price'],
						'pluginname'=>$item['pluginname']?:$item['onepluginname'],
					);
					if($nototal){
						unset($data['total']);
					}
					$data['uniacid']=$_SESSION['uniacid'];
					pdo_insert('jy_weishop_store_goods',$data);
					$this->up_options($item['id'],$storeid,$up);
			}elseif($up){
				$data=array(
					'goodsid'=>intval($item['id']),
					'storeid'=>intval($storeid),
					'total'=>intval($item['total']),
					'price'=>$item['price'],
					'pluginname'=>$item['pluginname']?:$item['onepluginname'],
				);
				if($nototal){
					unset($data['total']);
				}
				pdo_update('jy_weishop_store_goods',$data,array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$item['id'],'storeid'=>$storeid));
				$this->up_options($item['id'],$storeid,$up);
			}
		}
		return true;
	}
	/*
		添加修改商品
	*/
	public function editgoods($params){
		$data=array(
			'goodsid'=>intval($params['id']),
			'storeid'=>intval($params['storeid']),
			'total'=>intval($params['total']),
		);
		if($params['id']){
			$id=intval($params['id']);
			pdo_update('jy_weishop_store_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_weishop_store_goods',$data);
			$id=pdo_insertid();
		}
		return $id;
	}
	/*
		门店库存
	*/
	public function getinfo($goodsid,$storeid){
		$info=pdo_get('jy_weishop_store_goods',array('goodsid'=>$goodsid,'storeid'=>$storeid,'uniacid'=>$_SESSION['uniacid']),array('id','total','price'));
		$info['title']=pdo_getcolumn('jy_weishop_store',array('id'=>$storeid,'uniacid'=>$_SESSION['uniacid']),'title');
		return $info;
	}
	/*
		门店商品规格
	*/
	public function getoption($goodsid,$storeid,$specid='',$optionid=''){
		$params=array('goodsid'=>$goodsid,'storeid'=>$storeid,'uniacid'=>$_SESSION['uniacid']);
		if($specid && !$optionid){
			$optionid=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$goodsid,'specs'=>$specid,'uniacid'=>$_SESSION['uniacid']),'id');
		}
		if($optionid){
			$params['optionid']=$optionid;
		}
		$option=pdo_get('jy_weishop_store_goods_option',$params);
		if($option['optionid']){
			$option['id']=$option['optionid'];
			$sysdata=pdo_get('jy_weishop_goods_option',array('goodsid'=>$goodsid,'id'=>$option['optionid'],'uniacid'=>$_SESSION['uniacid']),array('price','thumb'));
			$option['price']=$option['price']>0?$option['price']:$sysdata['price'];
			$option['thumb']=$sysdata['thumb'];
		}
		return $option;
	}
	/*
		//更新客户归属
	*/
	public function up_memberid($storeid,$mid=0,$workerid=0){
		$mid=$mid?:$_SESSION['uid'];
		$storeset=$this->plugin_setting('storeset');
		if($storeset['open_saofen']){
			$_SESSION['sao_storeid']=$storeid;
		}
		$update=array();
		if($workerid){
			$update['workerid']=$workerid;
		}
		if($storeid){
			$update['storeid']=$storeid;
		}
		pdo_update('jy_weishop_member',$update,array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	/*
		//用户佣金，佣金
	*/
	public function up_commission($order,$status=0){
		$storeset=$this->plugin_setting('storeset');
		$member=pdo_get('jy_weishop_member',array('mid'=>$order['mid'],'merchid'=>0),array('storeid','workerid','mid','fromuser'));
		if(!$member['storeid'] && $storeset['open_storemid']){
			return false;
		}
		//门店独立设置
		$store=$this->get('store',array('id'=>$member['storeid'],'uniacid'=>$_SESSION['uniacid']),array('jifen_rate','order_rate','workersale'));
		if($store['jifen_rate']>0){
			$storeset['jifen_rate']=$store['jifen_rate'];
		}
		if($store['order_rate']>0){
			$storeset['order_rate']=$store['order_rate'];
		}
		if($member['workerid']){
			$this->up_workersale($order,$store,$status);
		}
		if(!$member['storeid'] && !$order['storeid']){
			return false;
		}
		if(!$member['storeid']){
			$member['storeid']=$order['storeid'];
		}
		if($storeset['open_saofen'] && $_SESSION['sao_storeid'] && $_SESSION['sao_storeid'] !=$member['storeid']){
			return false;
		}
		$order_price=$order['price'];
		$money=0;
		foreach($order['goods'] as $goods){
			$goodsid=$goods['goodsid'];
			$goods_commission=$this->getcolumn('goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['goodsid']),'commissions');
			$goods_commission=$goods_commission?json_decode($goods_commission,true):array();
			if(!$goods_commission['store_order_rate'] || $goods_commission['store_order_rate']<0){
				$needorder=true;
				continue;
			}else{
				$total_price=$goods['total_price']?$goods['total_price']:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id']),'total_price');
				if(substr($goods_commission['store_order_rate'],-1)=='%'){
						$goods_commission_money=($total_price*substr($goods_commission['store_order_rate'],0,-1))/100;
						$money+=$goods_commission_money;
				}else{
						$goods_commission_money=$goods_commission['store_order_rate']*$goods['total'];
						$money+=$goods_commission_money;
				}
				$order_price=$order_price-$total_price;
			}
		}
		if($needorder || !$goodsid){
			$commission_per=$storeset['order_rate'];
			if($commission_per>0){
				$money=intval($order['price']*$commission_per)/100;
			}
		}
		if($storeset['jifen_rate'] && $storeset['jifen_type']){
			$jifen=intval($order['price']*$storeset['jifen_rate'])/100;
		}
		if($money>0 || $jifen>0){
			$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'storeid'=>$member['storeid'],
						'mid'=>$order['mid'],
						'ordersn'=>$order['ordersn'],
						'money'=>$money,
						'total_price'=>$order['price'],
						'jifen'=>$jifen?:0,
						'jifen_type'=>$storeset['jifen_type'],
						'remark'=>'门店用户成交',
						'createtime'=>TIMESTAMP,
						'type'=>0,
						'status'=>$status,
			);
			$this->insert('store_commission',$new_commission);
		}
		return true;
	}
	/*
		//用户佣金，佣金
	*/
	public function up_workersale($order,$store=array(),$status=0){
		$storeset=$this->plugin_setting('storeset');
		if($store['workersale']>0){
			$storeset['workersale']=$store['workersale'];
		}
		$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid'],'merchid'=>0),array('storeid','workerid','mid'));
		if(!$member['storeid'] && $storeset['open_storemid']){
			return false;
		}
		if(!$member['workerid']){
			return false;
		}
		if($member['workerid']){
			$order_price=$order['price'];
			$money=0;
			foreach($order['goods'] as $goods){
				$goods_commission=$this->getcolumn('goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['goodsid']),'commissions');
				$goods_commission=$goods_commission?json_decode($goods_commission,true):array();
				if(!$goods_commission['store_workersale'] || $goods_commission['store_workersale']<0){
					$needorder=true;
					continue;
				}else{
					$total_price=$goods['total_price']?$goods['total_price']:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id']),'total_price');
					if(substr($goods_commission['store_workersale'],-1)=='%'){
							$goods_commission_money=($total_price*substr($goods_commission['store_workersale'],0,-1))/100;
							$money+=$goods_commission_money;
					}else{
							$goods_commission_money=$goods_commission['store_workersale']*$goods['total'];
							$money+=$goods_commission_money;
					}
					$order_price=$order_price-$total_price;
				}
			}
			if($needorder){
				$commission_per=$storeset['workersale'];
				if($commission_per>0){
					$money=intval($order['price']*$commission_per)/100;
				}
			}
			$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'storeid'=>$member['storeid'],
							'workerid'=>$member['workerid'],
							'mid'=>$order['mid'],
							'ordersn'=>$order['ordersn'],
							'money'=>$money,
							'total_price'=>$order['price'],
							'jifen'=>$jifen?:0,
							'remark'=>'门店用户成交',
							'createtime'=>TIMESTAMP,
							'type'=>2,
							'status'=>$status,
			);
			$this->insert('store_commission',$new_commission);
		}
		return true;
	}
	/*
		//开卡佣金
	*/
	public function up_workerka($order){
		$storeset=$this->plugin_setting('storeset');
		$member=$this->get('member',array('mid'=>$order['mid'],'merchid'=>0),array('storeid','workerid','mid'));
		if(!$member['workerid']){
			return false;
		}
		if($member['workerid']){
			$money=0;
			$commission_per=$storeset['workersale'];
			if(!$commission_per){
				return false;
			}
			$money=intval($order['fee']*$commission_per)/100;
			$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'storeid'=>$member['storeid'],
							'workerid'=>$member['workerid'],
							'mid'=>$order['mid'],
							'ordersn'=>$order['ordersn'],
							'money'=>$money,
							'total_price'=>$order['fee'],
							'remark'=>'会员开卡',
							'createtime'=>TIMESTAMP,
							'type'=>2,
							'status'=>1,
			);
			$this->insert('store_commission',$new_commission);
		}
		return true;
	}
	/*
		//完成订单
	*/
	public function complete_commission($order){
		$this->update('store_commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('status'=>0,'uniacid'=>$_SESSION['uniacid'],'ordersn'=>$order['ordersn']));
		return true;
	}
	/*
		创建店员结算单
	*/
	public function create_workerbalance($workerid,$post=array()){
		if(!$workerid){
			exi('请先选择要结算的店员！','error');
		}
		if($post['pay_type']=='downline'){
			if(!$post['wechat']){
				exi('请先填写微信号！','error');
			}
			if(!$post['mobile']){
				exi('请先填写联系电话！','error');
			}
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
		}
		//获取可结算佣金
		$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
		if($post['storeid']){
			$params['storeid']=$post['storeid'];
		}
		//店长结算
		if($post['isstore']){
			$params['type']=array(0,1,4,5,6,7);
		}else{
			$params['workerid']=$workerid;
			$params['type']=array(2,3);
		}
		$money=pdo_getsum('jy_weishop_store_commission',$params,'money');
		if($money<=0){
			exi('没有可结算的佣金！','error');
		}
		$commission_num=$this->count('store_commission',$params,'id');
		if($commission_num<=0){
			exi('没有可结算的佣金！','error');
		}
		$new_balance=array(
				'uniacid'=>$_SESSION['uniacid'],
				'balancesn'=>get_ordersn($this->tablename('store_commission'),'balancesn','AC'),
				'commission_num'=>$commission_num,
				'money'=>$money,
				'pay_type'=>$post['pay_type'],
				'realname'=>$post['realname'],
				'alipay'=>$post['alipay'],
				'bankname'=>$post['bankname'],
				'bankcard'=>$post['bankcard'],
				'wechat'=>$post['wechat'],
				'mobile'=>$post['mobile'],
				'createtime'=>TIMESTAMP,
				'status'=>0,
		);
		if($post['isstore']){
			$new_balance['storeid']=$post['storeid'];
			$new_balance['sendto']=1;
		}else{
			$new_balance['workerid']=$workerid;
			$new_balance['sendto']=2;
		}
		//结算手续费
		if($this->setting['balance_per']){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('store_balance',$new_balance);
		$balance_id=pdo_insertid();
		$update=array(
			'status'=>2,
			'balancesn'=>$new_balance['balancesn'],
		);
		$this->update('store_commission',$update,$params);
		return $balance_id;
	}
	/*
		创建结算单
	*/
	public function create_balance($isworker=false,$sendto=''){
		//获取可结算佣金
		if($isworker){
			$type=array(2,3);
		}else{
			$type=array(0,1);
		}
		$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'storeid >'=>0);
		$params['type']=$type;
		$money=pdo_getsum('jy_weishop_store_commission',$params,'money');
		if($money<=0){
			exi('没有可结算的佣金！','error');
		}
		$stores=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']));
		foreach($stores as $sv){
			$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'storeid'=>$sv['id']);
			$params['type']=$type;
			$commission_num=$this->count('store_commission',$params,'id');
			if($commission_num<=0){
				continue;
			}
			$money=pdo_getsum('jy_weishop_store_commission',$params,'money');
			$new_balance=array(
				'uniacid'=>$_SESSION['uniacid'],
				'storeid'=>$sv['id'],
				'balancesn'=>get_ordersn($this->tablename('store_commission'),'balancesn','AC'),
				'commission_num'=>$commission_num,
				'money'=>$money,
				'sendto'=>$sendto,
				'createtime'=>TIMESTAMP,
				'status'=>0,
			);
			//结算手续费
			if($this->setting['balance_per']){
				$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
			}else{
				$new_balance['balance_pay']=0;
			}
			$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
			$this->insert('store_balance',$new_balance);
			$balance_id=pdo_insertid();
			$update=array(
				'status'=>2,
				'balancesn'=>$new_balance['balancesn'],
			);
			$this->update('store_commission',$update,$params);
		}
		return true;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('store_balance',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no='',$sendto=0){
		$balance=$this->get('store_balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		if(!$sendto){
			$workers=pdo_get('jy_weishop_store_worker',array('status'=>1,'storeid'=>$balance['storeid'],'uniacid'=>$_SESSION['uniacid']),'mid','mid');
			if(count($workers)){
				$workers=array_keys($workers);
			}else{
				exi('该门店没有《有效店员》，打款失败！','error');
			}
			$final_money=$balance['final_money']/count($workers);
			foreach($workers as $mid){
				$trade_no=$this->$func_name($mid,$final_money,$balance['balancesn'],$wxgid);
				$this->model('notice')->notice_fenxiao_dakuan($mid,$final_money);
			}
		}elseif($sendto==1){
			$storeinfo=pdo_get('jy_weishop_store',array('id'=>$balance['storeid'],'uniacid'=>$_SESSION['uniacid']),'mid');
			if(!$storeinfo['mid']){
				exi('该门店没有《设置店长》！','error');
			}else{
				$trade_no=$this->$func_name($storeinfo['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
				$this->model('notice')->notice_fenxiao_dakuan($storeinfo['mid'],$balance['final_money']);
			}
		}elseif($sendto==2){
			$worker=pdo_get('jy_weishop_store_worker',array('id'=>$balance['workerid'],'uniacid'=>$_SESSION['uniacid']),'mid');
			if(!$worker['mid']){
				exi('该店员没有《绑定会员》！','error');
			}else{
				$trade_no=$this->$func_name($worker['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
				$this->model('notice')->notice_fenxiao_dakuan($worker['mid'],$balance['final_money']);
			}
		}
		$update=array(
			'status'=>3,
			'pay_trade_no'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
			'sendto'=>$sendto,
			'pay_time'=>TIMESTAMP,
		);
		if($pay_type){
			$update['pay_type']=$pay_type;
		}
		$this->update('store_balance',$update,array('id'=>$balance['id']));
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'门店佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($storeid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}
	//导入门店
	public function excel_stores($rows)
	{
			$num = count($rows);
			$i = 0;
			$colsIndex = array();
			foreach ($rows[1] as $cols => $col) {
				if ($col == 'title') {
					$colsIndex['title'] = $i;
				}

				if ($col == 'sku_barcode') {
					$colsIndex['sku_barcode'] = $i;
				}

				if ($col == 'price') {
					$colsIndex['price'] = $i;
				}

				if ($col == 'num') {
					$colsIndex['num'] = $i;
				}

				if ($col == 'description') {
					$colsIndex['description'] = $i;
				}

				if ($col == 'skuProps') {
					$colsIndex['skuProps'] = $i;
				}

				if ($col == 'picture') {
					$colsIndex['picture'] = $i;
				}

				if ($col == 'propAlias') {
					$colsIndex['propAlias'] = $i;
				}

				++$i;
			}
			$rows = array_slice($rows, 2, count($rows) - 2);
			$items = array();
			$num = 0;
			foreach ($rows as $rownu => $col) {
				$item = array();
				$item['title'] = $col[$colsIndex[title]];
				$item['marketprice'] = $col[$colsIndex[price]];
				$item['total'] = $col[$colsIndex[num]];
				$item['content'] = $col[$colsIndex[description]];
				$item['goodssn'] = $col[$colsIndex[sku_barcode]];
				$item['total'] = $col[$colsIndex[num]];
				$item['image'] = $col[$colsIndex[picture]];
				$item['video'] = $col[$colsIndex[video]];
				$this->create($params);
				$items[] = $item;
				++$num;
			}
			if(!$items[0]['title']){
				exi('上传文件失败：文件格式错误或文件内容为空！','error');
			}
		return true;
	}
	/*
		编号获取门店ID
	*/
	protected function getno_storeid($store_no){
		$store=pdo_get('jy_weishop_store',array('api_sid'=>$store_no,'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$store['id']){
			exi('门店编号不对，没找到该门店！','error');
		}
		return $store['id'];
	}
	protected function up_options($goodsid=0,$storeid=0,$isup=0){
		$options=$this->pdo_getall('goods_option',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']),array('id','stock','price','goodsid'));
		foreach($options as $ov){
			$isset=$this->pdo_getcolumn('store_goods_option',array('optionid'=>$ov['id'],'goodsid'=>$goodsid,'storeid'=>$storeid,'uniacid'=>$_SESSION['uniacid']),'id');
			if(!$isset){
				$data=array(
					'optionid'=>$ov['id'],
					'goodsid'=>$goodsid,
					'storeid'=>$storeid,
					'stock'=>$ov['stock'],
					'price'=>$ov['price'],
					'uniacid'=>$_SESSION['uniacid']
				);
				pdo_insert('jy_weishop_store_goods_option',$data);
			}elseif($isup){
				$data=array(
					'stock'=>$ov['stock'],
					'price'=>$ov['price'],
				);
				pdo_update('jy_weishop_store_goods_option',$data,array('id'=>$isset,'uniacid'=>$_SESSION['uniacid']));
			}
		}
		return '';
	}
	protected function delgoods($goodsid=0,$storeid=0){
		$params=array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']);
		if($storeid){
			$params['storeid']=$storeid;
		}
		pdo_delete('jy_weishop_store_goods',$params);
		pdo_delete('jy_weishop_store_goods_option',$params);
		return '';
	}
}